package com.budwk.app.sqfwV2.generator.weiqingmen.survey;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.weiqingmen.HouseHoldSurveyDTO;
import com.budwk.app.sqfwV2.dto.weiqingmen.InterviewDTO;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-08-02
 * <p>
 * 威清路入户调查 HouseHoldSurvey
 * 人员巡查记录.docx
 */
public class PersonnelInspectionPdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        return true;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/weiqingmen/survey/PersonnelInspection.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        HouseHoldSurveyDTO fieldDTO = (HouseHoldSurveyDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        List<String> careCategory = fieldDTO.getCareCategory();
        StringBuilder stringBuilder = new StringBuilder();
        for (String v : careCategory) {
            if (stringBuilder.length() == 0) {
                stringBuilder.append(v);
            } else {
                stringBuilder.append("、").append(v);
            }
        }
        paramMap.put("careCategory", stringBuilder.toString());

        // 时间、图片
        List<Map> pictures = new ArrayList<>();
        StringBuilder fillDateList = new StringBuilder();
        List<InterviewDTO> interviewDTOList = fieldDTO.getInterviewDTOList();
        interviewDTOList.sort(Comparator.comparing(InterviewDTO::getFillDate)); // 按时间升序
        paramMap.put("fillDate", DateUtil.format(interviewDTOList.get(interviewDTOList.size() - 1).getFillDate(), DatePattern.NORM_DATE_PATTERN));
        for (InterviewDTO dto : interviewDTOList) {
            // 时间
            if (fillDateList.length() == 0) {
                fillDateList.append(DateUtil.format(dto.getFillDate(), DatePattern.NORM_DATE_PATTERN));
            } else {
                fillDateList.append("、").append(DateUtil.format(dto.getFillDate(), DatePattern.NORM_DATE_PATTERN));
            }

            // 图片
            if (!StrUtil.isBlank(dto.getVisitPictures())) {
                String urls [] = StringUtils.isBlank(dto.getVisitPictures()) ? new String[0] : dto.getVisitPictures().split(",");
                for (String url : urls) {
                    Map<String, Object> pictureMap = Maps.newHashMap();
                    pictureMap.put("visitPictures", PictureUtils.scaleImage(url, 190, 210,false));
                    pictures.add(pictureMap);
                }
            }
        }
        paramMap.put("fillDateList", fillDateList.toString());
        paramMap.put("visitPicturess", pictures);
    }
}
